/**
 * @Author lilinjie
 * @Description: 通用创建、编辑hook
 */

import { computed, reactive } from 'vue'
import noop from '@/utils/noop'
import { useForm } from '@/hooks/common/useForm'
import { message } from 'ant-design-vue'

/**
 * 创建、编辑弹窗hook
 */
export function useCuModal({
  name = '',
  defaultCuFilterForm = {},
  create = noop,
  update = noop,
  getData = noop,
  onCreate = noop,
  isCloseModalOnCreate = true,
  onUpdate = noop,
  isCloseModalOnUpdate = true,
  handleModalClose = noop,
  // 打开弹窗时，是否获得数据
  isGetData = false
} = {}) {

  // 表单
  const { filterForm: cuFilterForm, clearForm: clearCuForm } = useForm({
    defaultFilterForm: { ...defaultCuFilterForm }
  })

  // 弹窗所需数据 - 私有
  const modelData = reactive({
    currentId: undefined,
    visible: false,
    title: name,
    isView: false
  })

  // 是否编辑
  const isEdit = computed(() => {
    console.log('isEdit', modelData.currentId)
    return modelData.currentId !== undefined
  })

  /**
   * 方法 - 设置当前ID
   */
  function setCurrentId(id) {
    modelData.currentId = id
  }

  /**
   * 方法 - 打开弹窗
   */
  function openModal() {
    modelData.visible = true
  }

  /**
   * 方法 - 关闭弹窗
   */
  function closeModal() {
    modelData.visible = false
    modelData.isView = false
    clearCuForm()
  }

  /**
   * 方法 - 设置表单数据
   */
  function setCuFormState(state) {
    Object.assign(cuFilterForm, state)
  }

  /**
   * 事件 - 点击创建
   */
  function onClickCreate() {
    setCurrentId(undefined)
    console.log(defaultCuFilterForm)
    modelData.title = `创建${name}`
    setCuFormState(defaultCuFilterForm)
    openModal()
  }

  /**
   * 事件 - 点击编辑
   */
  function onClickEdit(record) {
    console.log(record)
    setCurrentId(record.id)
    modelData.title = `编辑${name}`
    console.log(isGetData)

    if (isGetData) {
      getData({
        id: record.id,
        onSuccess(data) {
          setCuFormState(data)
          openModal()
        }
      })
    } else {
      setCuFormState(record)
      openModal()
    }
  }

  /**
   * 事件 - 点击确认
   */
  function onClickConfirm() {
    modelData.isView ? onClickClose() : submit()
  }

  /**
   * 方法 - 提交操作
   */
  function submit() {
    modelData.currentId
      ? update({
        cuFilterForm,
        currentId: modelData.currentId,
        onSuccess() {
          message.success(`更新${name}成功`)
          if (isCloseModalOnUpdate) {
            closeModal()
          }
          onUpdate()
        }
      })
      : create({
        cuFilterForm,
        onSuccess() {
          message.success(`创建${name}成功`)
          if (isCloseModalOnCreate) {
            closeModal()
          }
          onCreate()
        }
      })
  }

  /**
   * 点击关闭
   */
  function onClickClose() {
    setCurrentId(undefined)
    clearCuForm()
    console.log('onClickClose', modelData)
    closeModal()
  }

  /**
   * 点击查看
   */
  function onClickView(record) {
    modelData.isView = true
    setCuFormState(record)
    openModal()
  }

  return {
    cuFilterForm,
    modelData,
    isEdit,
    onClickClose,
    onClickConfirm,
    onClickCreate,
    onClickEdit,
    onClickView
  }
}
